﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 坦克大战正式
{
    class EnemyTank:Movething
    {
        public int ChangeDirSpeed { get; set; }
        private int changDirCount = 0;
        public int AttackSpeed { get; set; }
        private int attackCount = 0;
        private Random r = new Random();
        public EnemyTank(int x, int y, int speed,Bitmap bmpdDown,Bitmap bmpUp,Bitmap bmpRught,Bitmap bmpLeft)
        {
            //IsMoving = true;
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            BitmapDown = bmpdDown;
            BitmapLeft = bmpLeft;
            BitmapRight = bmpRught;
            BitmapUp = bmpUp;
            this.Dir = Direction.Down;
            AttackSpeed = 60;
            ChangeDirSpeed = 70;
        }
        public override void Update()
        {
            MoveCheck();//移动检查
            Move();
            AttackCheck();
            AutoChangeDirection();

            base.Update();
        }
        private void MoveCheck()
        {
            #region 检查有没有超出窗体边界
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Speed > 450 - Height)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Speed > 450 - Width)
                {
                    ChangeDirection(); return;
                }
            }
            #endregion

            //检查有没有和其它元素发生碰撞
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speed;
                    break;
                case Direction.Down:
                    rect.Y += Speed;
                    break;
                case Direction.Right:
                    rect.X += Speed;
                    break;
                case Direction.Left:
                    rect.X -= Speed;
                    break;
            }
            if (GameObjectManager.IsCollideWall(rect) != null)
            {
                ChangeDirection(); return;
            }
            if (GameObjectManager.IsCollideSteel(rect) != null)
            {
                ChangeDirection(); return;
            }
            if (GameObjectManager.IsCollideBoss(rect))
            {
                ChangeDirection(); return;
            }
        }

        private void AutoChangeDirection()
        {
            changDirCount++;
            if (changDirCount < ChangeDirSpeed) return;
            ChangeDirection();
            changDirCount = 0;
        }
        private void ChangeDirection()
        {
            while(true)
            {
                Direction dir = (Direction)r.Next(0, 4);
                if(dir == Dir)
                {
                    continue;
                }
                Dir = dir;
                break;
            }
            MoveCheck();
        }
        public void Move()
        {
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
            }
        }
        private void AttackCheck()
        {
            attackCount++;
            if (attackCount < AttackSpeed) return;
            Attack();
            attackCount = 0;
        }
        private void Attack()
        {
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + Width / 2;
                    break;
                case Direction.Down:
                    x = x + Width / 2;
                    y = y + Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.Right:
                    x = x + Width;
                    y = y + Height / 2;
                    break;

            }
            //GameObjectManager.CreateBullet(x-Width/2, y-Height/2, Tag.MyTank, Dir);
            GameObjectManager.CreateBullet(x, y, Tag.EnemyTank, Dir);

        }
    }
}
